# Board MMIO Configuration:
#   0  leds
#   1  switches
#   2  seven segs
#   3  keypad

######################
### "KERNEL" SPACE ###

# initialise coprocessor0
addi $k0, $0, 0x0C     # isr address
mtc0 $k0, $15          # set ebase to isr address

# go to user code
j start

### interrupt service routine
isr: mfc0 $k0, $13     # get the cause register

andi $k1, $k0, 0x2000  # mask keypad interrupt bit
beq $k1, $0, isr0      # if keypad, jump to handler
jal keypad             # handle keypad and return

isr0:                  # continuing
andi $k1, $k0, 0x0800  # mask switches interrupt bit
beq $k1, $0, isr1      # if switches, jump to handler
jal switches           # handle switches and return
isr1: eret             # return from interrupt, nothing else handled

## switches interrupt service subroutine
switches: lw $k1, ($s1)# load switches



sw $k1, ($s0)          # set to leds
# set to interrupt step instead



jr $ra

####################
### "USER" SPACE ###

start:
#addi $k0, $0, 0x2801   # create status word
#mtc0 $k0, $12          # set the status word

# initialise memory offsets
lui $s0, 0xFFFF        # set base address for leds/for all MMIO
addi $s1, $s0, 0x0040  # set base address of the switches controller
addi $s2, $s0, 0x0080  # set base address for seven seg controller
addi $s3, $s0, 0x00C0  # set base address of the hex keypad controller

# initialise seven segs
addi $t0, $0, 0x000F   # turn all seven segs on
lui $s4, 0x00F0        # set mask for write enables and no dots
sw $t0, 0($s2)         # turn all seven segs on
sw $s4, 4($s2)         # write zeros without dots

# initialise keypad
#addi $t0, $0, 4        # only want every four presses
#sw $t0, 0xC($s3)       # set hex keypad interrupt step

### main program
loop: jal keypad
j loop           # wait forever, no rergular programming

## keypad subroutine
keypad: lw $t0, 0($s3) # load hexkeypad buffer
andi $t0, $t0, 0xFFFF  # get only latest four buttons
add $t0, $t0, $s4      # add seven seg data (write/dots off)
sw $t0, 4($s2)         # write to seven segs
jr $ra                 # return from subroutine
